package com.zzf.concurrent.example.atomic.cas.lock;

import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.IntStream;

/**
 * @author GaoFeng2017
 * @date 2019/9/12 10:41
 */
@Slf4j
public class App {
    public static void main(String[] args) {
        CompareAndSetLock lock = new CompareAndSetLock();
        IntStream.rangeClosed(1, 5).forEach(index -> {
            new Thread(() -> {
                try {
                    lock.tryLock();
                    log.info("{} is get lock.", Thread.currentThread().getName());
                    TimeUtil.sleep(5000);
                } finally {
                    lock.unlock();
                }

            }, "Thread" + index).start();
        });
    }
}
